import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/account/api/accountApi.dart';
import 'package:honeypotflutter/modules/account/page/account_withdraw_amount_success/view.dart';
import 'package:honeypotflutter/public/public_get.dart';
import 'package:honeypotflutter/publicBussine/loginInfo/bussine_userInfo.dart';
import 'package:honeypotflutter/public/public_total.dart';

class AccountWithdrawAmountLogic extends GetxController {
  UserInfoLogic userInfoLogic = Get.find<UserInfoLogic>();

  RxString serverMonry = RxString("0.00");
  RxString inComeMonry = RxString("0.00");

  final TextEditingController controller = TextEditingController();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    controller.addListener(() {
      if (controller.text.isEmpty) {
        return;
      }
      double monry = double.parse(controller.text);
      double nowServerMonry = monry * (userInfoLogic.userInfo.withdrawFeeRate ?? 0.00);
      serverMonry.value = nowServerMonry.toStringAsFixed(2);
      inComeMonry.value = (monry - nowServerMonry).toStringAsFixed(2);
    });
  }

  /*
  * 确认
  * */
  sureAction() async {
    if (controller.text.isEmpty || double.parse(controller.text) <= 0.0) {
      HLTotal.total("请输入金额");
      return;
    }
    double inputMonry = double.parse(controller.text);
    double wallet = userInfoLogic.userInfo.wallet ?? 0.00;
    if (wallet <= 0.0) {
      HLTotal.total("可提现金额为0.00元");
      return;
    }
    if (inputMonry > wallet) {
      HLTotal.total("提现金额不能大于账户余额");
      return;
    }

    try {
      HLTotal.progress(status: "提交中...");
      await AccountApi.transferBalance(amount: controller.text, type: 4);
      HLGet.off(AccountWithdrawAmountSuccessPage(), arguments: controller.text);
    } catch (error) {
      HLTotal.dismiss();
    }
  }
}
